<template>
  <div></div>
</template>
<script>
  import {jsLink} from '../utils/public'
  import {alipayExitApp} from '../utils/alipayJsApi'
  import {getUser} from '../utils/http'

  const {alipayAuthBaseUrl, getAuthCodeUrl} = global.lineConfig.api
  const {appId, scope} = global.lineConfig.alipayCardInfo
  export default {
    mounted() {
      this.onReady()
    },
    methods: {
      onReady() {
        const {auth_code, redirectUrl} = this.$route.query
        if (auth_code === undefined) this.noAuthCode(redirectUrl)
        else this.inAuthCode(auth_code, redirectUrl)
      },
      noAuthCode() {
        // 进入支付宝授权获取auth_code
        jsLink('replace', `${alipayAuthBaseUrl}?app_id=${appId}&scope=${scope}&redirect_uri=${encodeURIComponent(getAuthCodeUrl)}`)
      },
      inAuthCode(auth_code, redirectUrl) {
        if (sessionStorage.getItem('isClose') === 'yes') alipayExitApp()  // 解决用户无限返回bug
        getUser({
          auth_code: auth_code,
          cb: data => {
            sessionStorage.setItem('userId', data.userId || '')
            // 判断是否进入线路提醒设置页面
            if (redirectUrl === 'fromSelf') this.$router.replace('/lineSet?from=self')
            else this.$router.replace(`/${redirectUrl}`)
          }
        })
      }
    }
  }
</script>
